﻿@model IPagedList<Models.DataModel.Company>
@{Models.DataStore.Ctrl ctrlModel = new Models.DataStore.Ctrl();
    var lstCountry = new Dictionary<string, string>();
    var lst = ctrlModel.GetListCountry();
    foreach (var c in lst)
    {
        lstCountry.Add(c.CountryId.ToString(), c.Name);
    }
}
@using PagedList
@using PagedList.Mvc
@{
    ViewBag.Title = "Company";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>

</div>
<br />

<form class="form-inline">
    <div class="form-group">
        <input class="form-control" placeholder="Please input company name" type="text" id="txtSearch" onkeyup="Search(this.value);" />
    </div>
    <button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("AddCompany", "Company")'">New Company</button>
    <a href="@Url.Action("ExportCompany","Company")" class="btn btn-default">Export Excel</a>
</form>
<br />
<div class="table-responsive">
    <table class="table table-bordered table-hover table-condensed">
        <thead>
            <tr>
                <th>
                    <input id="ChkCheckFull" name="DeleteAll" type="checkbox" value="">
                    <a href="" id="btnDeleteAll" class="glyphicon glyphicon-trash" style="color:red; border:none; background:none"></a>
                </th>
                <th>Company Number</th>
                <th>Name</th>
                <th>Address 1</th>
                <th>City</th>
                <th>ZipCode</th>
                <th>State / Province</th>
                <th>Country Name</th>
                <th>CreateOn</th>
                <th>ModifiedByName</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @{ int i = 0;
                foreach (var item in Model)
                {
                    i++;
                    var style = i % 2 == 0 ? "gradeA even" : "gradeA odd";
                    <tr class="@style" id=@item.CompanyId>
                        <td><input name="delvalue" type="checkbox" value="" class="checkitem" id="@item.CompanyId"></td>
                        <td><a href="@Url.Action("EditCompany", "Company", new { CompanyId = item.CompanyId })">@item.CompanyNumber</a></td>
                        <td>@item.Name</td>
                        <td class="center sorting_1">@item.Address1</td>
                        <td class="center">@item.City</td>
                        <td class="center">@item.ZipCode</td>
                        <td>@item.StateOrProvice</td>
                        <td>@ctrlModel.GetCountryById(item.CountryId.Value).Name</td>
                        <td>@item.CreatedOn</td>
                        <td>@item.ModifiedByName</td>
                        <td><a href="javascript:DeleteCompany('@item.CompanyId',false);">Delete</a></td>
                    </tr>
                }
            }
        </tbody>
    </table>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 padding nagavition">
    @if (Model.PageCount > 1)
    {
        @Html.PagedListPager(Model, page => Url.Action("Index", new
   {
       page,
       searchtext = ViewBag.SearchText,
   }))
    }
</div>

<script>
    $("#ChkCheckFull").click(function () {
        if ($("#ChkCheckFull").is(':checked') == true) {
            $(".checkitem").prop('checked', true);
        }
        else {
            $(".checkitem").prop('checked', false);
        }

    });
    $("#btnDeleteAll").click(function () {
        var lstData = "";
        $('.checkitem').each(function () {
            var sThisVal = (this.checked ? "1" : "0");
            if (sThisVal == 1) {
                lstData += $(this).attr('id') + "|";
            }
        });
        DeleteCompany(lstData, true);
    });

    function DeleteCompany(companyID, multiple) {
        if (confirm('Are you sure you wish to delete these records?')) {
            $.ajax({
                type: "POST",
                async: false, //phai co cai nay moi return data trong ajax duoc
                url: "/Company/DeleteCompany",
                data: JSON.stringify({ view_companyId: companyID }),
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    if (data == 1) {
                        if (multiple) {
                            var arrCompanyId = companyID.split('|');
                            $.each(arrCompanyId, function (index, item) {
                                $("#" + item).remove();
                            });
                        }
                        else {
                            //console.log($("#" + companyID));
                            $("#" + companyID).remove();
                        }
                    }
                },
                error: function (xhr, testStatus, error) {
                    alert("error");
                }
            });
        }
    }

    function Search(keyword) {
        var lstCountry = @Html.Raw(Json.Encode(lstCountry));
        $("tbody>tr").remove();
        $.ajax({
            type: "POST",
            async: false, //phai co cai nay moi return data trong ajax duoc
            url: "/Company/SearchCompany",
            data: JSON.stringify({ view_keyword: keyword }),
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                if (data.length > 0) {
                    $.each(data, function (index, item) {
                        var style = (index + 1) % 2 == 0 ? "gradeA even" : "gradeA odd";
                        var strNewRow = "";
                        strNewRow += "<tr class='" + style + "' id='" + item.CompanyId + "'>";
                        strNewRow += "<td><input name='delvalue' type='checkbox' value='' class='checkitem' id='" + item.CompanyId + "'></td>";
                        strNewRow += "<td><a href='/Company/EditCompany?CompanyId=" + item.CompanyId + "'>" + item.CompanyNumber + "</a></td>";
                        @*strNewRow += "<td><a href='@Url.Action("EditCompany", "Company", new { CompanyId = "JAV" })'>" + item.CompanyNumber + "</a></td>";
                            strNewRow = strNewRow.replace("JAV", item.CompanyId);*@
                        strNewRow += "<td>" + item.Name + "</td>";
                        strNewRow += "<td class='center sorting_1'>" + (!item.Address1 ? "N/A" : item.Address1) + "</td>";
                        strNewRow += "<td class='center'>" + (!item.City ? "N/A" : item.City) + "</td>";
                        strNewRow += "<td class='center'>" + (!item.ZipCode ? "N/A" : item.ZipCode) + "</td>";
                        strNewRow += "<td>" + (!item.StateOrProvice ? "N/A" : item.StateOrProvice) + "</td>";
                        strNewRow += "<td>" + lstCountry[item.CountryId] + "</td>";
                        strNewRow += "<td>" + ToJavaScriptDate(item.CreatedOn) + "</td>";
                        strNewRow += "<td>" + item.ModifiedByName + "</td>";
                        strNewRow += "<td><a href='javascript:DeleteCompany('" + item.CompanyId + "',false);'>Delete</a></td>";
                        strNewRow += "</tr>";
                        $("tbody").append(strNewRow);
                    });
                }
            },
            error: function (xhr, testStatus, error) {
                alert("error");
            }
        });
    }

    function ToJavaScriptDate(value) {
        var pattern = /Date\(([^)]+)\)/;
        var results = pattern.exec(value);
        var dt = new Date(parseFloat(results[1]));
        return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear() + " " + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
    }
</script>
